var app = getApp()
Page({
  data: {
    list:[],
    priceName:'进货价',
    addImg:'../../../image/add.png',
    retailPrice:true,
    page:2,
    topBtn:[],
    topCurrentId:'all',
    leftCurrentId:'',
    leftBtn:[],
    parent_id:0,
    goodsType:0,
    parentType:0,
    settlementCount:'',
    searchKey:'',
    accountId:''
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
      this.getFirstTypeList(res.data.agentId)
      this.getGoodsTypeList(res.data.agentId,0)
      this.getSettlementCount(res.data.agentId,res.data.id)
    })
  },
  onShow(){
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
      this.getSettlementCount(res.data.agentId,res.data.id)
    })
  },
  onReachBottom(){
    let page = this.data.page
    this.getGoodsList(page,this.data.searchKey)
    page++
    this.setData({
      page
    })
  },
  searchInput(e){
    let searchKey = e.detail.value
    this.setData({
      searchKey
    })
  },
  searchConfirm(){
    this.getGoodsList(1,this.data.searchKey)
  },
  //获取商品列表
  getGoodsList(pageNum,searchKey){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      list:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      parentType:this.data.parentType,
      pageNum,
      searchKey:this.data.searchKey,
    }
    let list = []
    console.log(data)
    app.request('wx_app/product/getAllProductList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.product_name,
            price:item.set_price3==null?item.set_price2==null?item.set_price1:item.set_price2:item.set_price3,
            stockPrice:item.stockPrice==null?0:item.stockPrice,
            url:item.main_img_url,
            id:item.id,
            color:item.tag_color,
            sales:item.sales,
            companyName:item.product_tag,
            deliberation:item.deliberation,
            manufacturer_id:item.manufacturer_id,
            product_type:item.product_type,
            version_name_id:item.version_name_id,
            version_values:item.version_values,
            set_price:item.set_price3==null?item.set_price2==null?item.set_price1:item.set_price2:item.set_price3
          })
          that.setData({
            list
          })
        })
        wx.hideLoading()
        if (that.data.list.length==0) {
          that.setData({
            noData:true
          })
        }else{

          that.setData({
            noData:false
          })
        }
      }else{
        that.setData({
          noData:true
        })
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  //商品详情
  bindToDetail(e){
    wx.navigateTo({
      url:'purchaseDetail?type=2&goodsType='+this.data.goodsType+
          '&id='+e.currentTarget.dataset.id+
          '&color='+e.currentTarget.dataset.color+
          '&company='+e.currentTarget.dataset.company
    })
  },
  chooseAll(){
    this.setData({
      topCurrentId:'all',
      parent_id:0,
      leftCurrentId:0,
      goodsType:0
    })
    this.getGoodsTypeList(this.data.agentId,0)
  },
  chooseFirstType(e){
    let id = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    let parent = e.currentTarget.dataset.parent
    this.setData({
      topCurrentId:id,
      parent_id:type,
      leftCurrentId:0,
      page:2,
      goodsType:type,
      parentType:0,
    })
    this.getGoodsTypeList(this.data.agentId,type)
  },
  bindLeftBtn(e){
    let type = e.currentTarget.dataset.type
    let parent = e.currentTarget.dataset.parent
    console.log(parent)
    if (parent==0) {
      if (this.data.leftCurrentId==0) {
        //全部 全部
        this.setData({
          goodsType:0,
          parentType:0,
        })
        console.log('全部 全部')
      }else{
        //其他 全部
        this.setData({
          goodsType:this.data.parent_id,
          parentType:0,
        })
        console.log('其他 全部')
      }
    }else{
      //其他 其他
      this.setData({
        goodsType:type,
        parentType:parent,
      })
      console.log('其他 其他')
    }
    this.setData({
      leftCurrentId:e.currentTarget.dataset.id,
      page:2
    })
    this.getGoodsList(1,this.data.searchKey)
  },
  //获取一级分类
  getFirstTypeList(agentId){
    const that = this
    let topBtn = this.data.topBtn
    let data = {
      agentId,
      requestType:'agent'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      // topBtn.push({name:"整车类",id:2,parent:0})
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0&&item.id!=982) {
          console.log(item)
          topBtn.push({name:item.name,id:item.id,parent:item.parent_id})
        }
      })
      that.setData({
        topBtn
      })
    })
  },
  //获取商品种类
  getGoodsTypeList(agentId,type){
    const that = this
    let leftBtn = [],
        backBox = [],
        goodsType='',
        parentType='',
    data = {
      agentId,
      requestType:'agent'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      leftBtn.push({name:'全部',id:0,parent:0})
      res.data.product_types.forEach(item=>{
        if (type==item.parent_id&&type!=0) {
          leftBtn.push({name:item.name,id:item.id,parent:item.parent_id})
        }else if (type==0) {
          if (item.parent_id!=0) {
            leftBtn.push({name:item.name,id:item.id,parent:item.parent_id})
          }
        }
        if (type==item.id) { 
          backBox.push({name:item.name,id:item.id,parent:item.parent_id})
        }
      })
      that.setData({
        leftBtn,
        backBox
      })
      if (that.data.leftBtn==''&&that.data.topCurrentId=='all') {
        res.data.product_types.forEach(item=>{
          leftBtn.push({name:item.name,id:item.id})
        })
        that.setData({
          leftBtn
        })
      }
      if (that.data.leftBtn=='') {
        that.setData({
          leftBtn:that.data.backBox
        })
      }
      // goodsType = that.data.leftBtn[0].id
      // parentType = that.data.leftBtn[0].parent
      // that.setData({
      //   goodsType,
      //   parentType
      // })
      that.getGoodsList(1,this.data.searchKey)
    })
  },
  bindMore(){
    wx.showLoading({
      title:'正在加载'
    })
    let page = this.data.page
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:page,
      parentType:this.data.parentType,
      searchKey:''
    }
    let list = this.data.list
    console.log(data)
    app.request('wx_app/product/getAllProductList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.product_name,
            price:item.set_price3==null?item.set_price2==null?item.set_price1:item.set_price2:item.set_price3,
            stockPrice:item.stockPrice==null?0:item.stockPrice,
            url:item.main_img_url,
            id:item.id,
            color:item.tag_color,
            sales:item.sales,
            companyName:item.product_tag,
            deliberation:item.deliberation,
            manufacturer_id:item.manufacturer_id,
            product_type:item.product_type,
            version_name_id:item.version_name_id,
            version_values:item.version_values,
            set_price:item.set_price3==null?item.set_price2==null?item.set_price1:item.set_price2:item.set_price3
          })
          that.setData({
            list
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      page:page
    })
  },
  //获取购物车数量
  getSettlementCount(agentId,accountId){
    let data = {
      agentId,
      accountId
    }
    console.log(data)
    app.request('wx_app/stock_cart/queryCartListByAgentId',data,'GET').then(res=>{
      console.log(res)
      let count = 0
      res.data.forEach(item=>{
        item.carts.forEach(_item=>{
          count+=_item.count
        })
      })
      this.setData({
        settlementCount:count
      })
    })
  },
  bindToSettlement(){
    wx.navigateTo({
      url: 'purchaseSettlement'
    })
  },
  addGoods(e){
    let current = e.currentTarget.dataset
    let data = {
      agentId:this.data.agentId,
      productType:current.type,
      stockSkuId:current.id,
      productName:current.name,
      price:current.stockPrice==null?0:current.stockPrice,
      thumbUrl:current.url,
      count:1,
      manufacturerId:current.mId,
      accountId:this.data.accountId,
      versionValues:current.vValues,
      versionNameId:current.vId
    }
    console.log(data)
    app.request('wx_app/stock_cart/saveStockCart',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        app.errorMsg('加入进货单成功')
        this.getSettlementCount(this.data.agentId,this.data.accountId)
      }else{
        app.errorMsg(res.message)
      }
    })
  }
})